package zhoufu.sz.sanxiao_check.ui.adapter.items;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.List;

import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
import eu.davidea.flexibleadapter.items.IHeader;
import eu.davidea.viewholders.ExpandableViewHolder;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.ui.adapter.FlexibleDBAdapter;
import zhoufu.sz.sanxiao_check.utils.UIUtils;

/**
 * 项目： FlexibleAdapter
 * 时间： 2016/6/22 10:12
 * 人员： hyc
 * 功能：
 */

public abstract class DataBindingItem<DB> extends AbstractFlexibleItem<DataBindingItem.ViewHolder> {

    private DB name;
    private IHeader _header;

    public DataBindingItem(DB s, IHeader header) {
        name = s;
        _header = header;
    }

    public DB getDB() {
        return name;
    }

    public void setDB(DB name) {
        this.name = name;
    }

    public DataBindingItem() {

    }

    @Override
    public void bindViewHolder(FlexibleAdapter adapter, ViewHolder holder, int position, List payloads) {

        if (payloads.isEmpty() || hasNoDataBindingInvalidate(payloads)) {
            setVariable(adapter, holder, position, payloads);
        }
        holder._dataBinding.executePendingBindings();

    }

    protected void setVariable(FlexibleAdapter adapter, ViewHolder holder, int position, List payloads) {

    }

    private boolean hasNoDataBindingInvalidate(List payloads) {
        for (Object obj : payloads) {
            if (obj != FlexibleDBAdapter.DB_PAYLOAD) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int getLayoutRes() {
        return 0;
    }


    @Override
    public ViewHolder createViewHolder(FlexibleAdapter adapter, LayoutInflater inflater, ViewGroup parent) {

        ViewDataBinding inflate = DataBindingUtil.inflate(inflater, getLayoutRes(), parent, false,
                LoginComponet.Instance.get());

        return new ViewHolder<>(inflate, adapter);
    }

    public static final class ViewHolder<DB extends android.databinding.ViewDataBinding> extends ExpandableViewHolder {

        public final DB _dataBinding;

        /**
         * Default constructor.
         *
         * @param dataBinding The {@link ViewDataBinding} being hosted in this ViewHolder
         * @param adapter     Adapter instance of type {@link FlexibleAdapter}
         */
        public ViewHolder(DB dataBinding, FlexibleAdapter adapter) {
            super(dataBinding.getRoot(), adapter);
            _dataBinding = dataBinding;
        }

        @Override
        public float getActivationElevation() {

            return UIUtils.dip2px(4);

        }

        @Override
        protected void expandView(int position) {
            super.expandView(position);
            if (mAdapter.isExpanded(position)) mAdapter.notifyItemChanged(position, true);

        }

        @Override
        protected void collapseView(int position) {
            super.collapseView(position);
            if (!mAdapter.isExpanded(position)) mAdapter.notifyItemChanged(position, true);

        }
    }
}